package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Consulta;


public class DAOConsulta implements InterfaceDAOConsulta{

	public void createConsulta(Consulta consulta, Integer cod) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Consulta "
				+ "(CodUsuario, DataConsulta, Hospital_Clinica, Medico, Diagnostico, DataVoltaConsulta) "
				+ "values (?,?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, cod);
			stmp.setString(2, consulta.getDataConsulta());
			stmp.setString(3, consulta.getHospitalClinica());
			stmp.setString(4, consulta.getMedico());
			stmp.setString(5, consulta.getDiagnostico());
			stmp.setString(6, consulta.getDataVolta());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	public ArrayList<Consulta> readConsulta(Integer cod) throws SQLException {
		Connection con = new ConnectionFactory().getConnection();
		String seleciona = " where CodUsuario="+Integer.toString(cod);
		String sql = "Select * from Consulta "+seleciona;

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Consulta> lista = new ArrayList<Consulta>();
			while (rs.next()) {
				Consulta consulta = new Consulta();
				consulta.setDataConsulta(rs.getString("DataConsulta"));
				consulta.setHospitalClinica(rs.getString("Hospital_Clinica"));
				consulta.setMedico(rs.getString("Medico"));
				consulta.setDiagnostico(rs.getString("Diagnostico"));
				consulta.setDataVolta(rs.getString("DataVoltaConsulta"));
				consulta.setCod(rs.getInt("idConsulta"));
				lista.add(consulta);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	public void updateConsulta(Consulta consulta) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Consulta set DataConsulta=?"
				+ " ,Hospital_Clinica=?" + " ,Medico=?"
				+ " ,Diagnostico=?" + " ,DataVoltaConsulta=?"
				+ "where idConsulta=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, consulta.getDataConsulta());
			stmp.setString(2, consulta.getHospitalClinica());
			stmp.setString(3, consulta.getMedico());
			stmp.setString(4, consulta.getDiagnostico());
			stmp.setString(5, consulta.getDataVolta());
			stmp.setInt(6, consulta.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	public void deleteConsulta(Consulta consulta) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Consulta where idConsulta=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, consulta.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

}
